﻿using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using My.Admin.Dto.User;
using My.Admin.Model;
using My.Admin.Model.System;
using My.Admin.Service.System;
using My.Admin.Service.System.IService;
using My.Admin.Util;
using My.Core.CustomException;
using My.Core.DynamicWebApi.Attributes;
using My.Core.Enums;
using My.Core.SqlSugar.model;
using System.ComponentModel;

namespace My.Admin.Dynamic
{
	[Service("SysUserController")]
	public class SysUserController
	{
		private readonly ISysUserService _sysUserRep;
		private readonly ISysUserRoleService _sysUserRoleRep;

		public SysUserController(ISysUserService sysUserRep, ISysUserRoleService sysUserRoleRep)
		{
			_sysUserRep = sysUserRep;
			_sysUserRoleRep = sysUserRoleRep;
		}

		/// <summary>
		/// 分页查询用户
		/// </summary>
		/// <returns></returns>
		[Authorize]
		[HttpPost]
		public async Task<PagedInfo<SysUser>> Page(PageUserInput input)
		{
			return await _sysUserRep.Page(input);
		}

		/// <summary>
		/// 增加用户
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[HttpPost]
		public async Task<long> AddUser(AddUserInput input)
		{
			var password = "111111";

			MD5Encryption.Encrypt(password, true, false);
			var user = input.Adapt<SysUser>();
			user.Password = MD5Encryption.Encrypt(password, true, false);
			var newUser = await _sysUserRep.AsInsertable(user).ExecuteReturnEntityAsync();
			input.Id = newUser.Id;
			await UpdateRoleAndExtOrg(input);

			return newUser.Id;
		}

		/// <summary>
		/// 删除用户
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[HttpPost]
		public async Task DeleteUser(DeleteUserInput input)
		{
			var user = await _sysUserRep.GetFirstAsync(u => u.Id == input.Id) ?? throw new CustomException(ResultCode.FAIL, "数据库操作失败", "数据库操作失败");

			await _sysUserRep.DeleteAsync(user);

			// 删除用户角色
			await _sysUserRoleRep.DeleteUserRoleByUserId(input.Id);
		}

		/// <summary>
		/// 更新用户
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		/// <exception cref="CustomException"></exception>
		[Authorize]
		[HttpPost]
		public async Task UpdateUser(UpdateUserInput input)
		{
			if (await _sysUserRep.AsQueryable().Filter(null, true).AnyAsync(u => u.Account == input.Account && u.Id != input.Id))
				throw new CustomException(ResultCode.FAIL, "数据库操作失败", "数据库操作失败");

			await _sysUserRep.AsUpdateable(input.Adapt<SysUser>()).IgnoreColumns(true).ExecuteCommandAsync();

			await UpdateRoleAndExtOrg(input);
		}

		/// <summary>
		/// 获取用户拥有角色集合
		/// </summary>
		/// <param name="userId"></param>
		/// <returns></returns>
		[Authorize]
		[HttpGet]
		[DisplayName("获取用户拥有角色集合")]
		public async Task<List<long>> GetOwnRoleList(long userId)
		{
			return await _sysUserRoleRep.GetUserRoleIdList(userId);
		}

		/// <summary>
		/// 更新角色和扩展机构
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		private async Task UpdateRoleAndExtOrg(AddUserInput input)
		{
			await GrantRole(new UserRoleInput { UserId = input.Id, RoleIdList = input.RoleIdList });
		}

		/// <summary>
		/// 授权用户角色
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		public async Task GrantRole(UserRoleInput input)
		{
			await _sysUserRoleRep.GrantUserRole(input);
		}
	}
}
